package com.ma.middleware.dynamic.thread.domain;

import com.alibaba.fastjson.JSON;
import com.ma.middleware.dynamic.thread.domain.model.entity.ThreadPoolConfigEntity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ThreadPoolExecutor;

public class DynamicThreadPoolService implements IDynamicThreadPoolService{
    private final Logger logger = LoggerFactory.getLogger(DynamicThreadPoolService.class);
    private final String applicatioName;
    private final Map<String, ThreadPoolExecutor> threadPoolExecutorMap;

    public DynamicThreadPoolService(String applicatioName, Map<String, ThreadPoolExecutor> threadPoolExecutorMap) {
        this.applicatioName = applicatioName;
        this.threadPoolExecutorMap = threadPoolExecutorMap;
    }

    @Override
    public List<ThreadPoolConfigEntity> queryThreadPoolList() {
        Set<String> threadPoolBeanNames = threadPoolExecutorMap.keySet();
        ArrayList<ThreadPoolConfigEntity> threadPoolVos = new ArrayList<>(threadPoolBeanNames.size());
        for (String beanName : threadPoolBeanNames) {
            ThreadPoolExecutor threadPoolExecutor = threadPoolExecutorMap.get(beanName);
            ThreadPoolConfigEntity threadPoolConfigVo = new ThreadPoolConfigEntity(applicatioName, beanName);
            threadPoolConfigVo.setCorePoolSize(threadPoolExecutor.getCorePoolSize());
            threadPoolConfigVo.setMaximumPoolSize(threadPoolExecutor.getMaximumPoolSize());
            threadPoolConfigVo.setPoolSize(threadPoolExecutor.getPoolSize());
            threadPoolConfigVo.setQueueType(threadPoolExecutor.getQueue().getClass().getSimpleName());
            threadPoolConfigVo.setQueueSize(threadPoolExecutor.getQueue().size());
            threadPoolConfigVo.setRemainingCapacity(threadPoolExecutor.getQueue().remainingCapacity());
            threadPoolVos.add(threadPoolConfigVo);
        }
        return threadPoolVos;
    }

    @Override
    public ThreadPoolConfigEntity queryThreadPoolConfigByName(String threadPoolName) {
        ThreadPoolExecutor threadPoolExecutor = threadPoolExecutorMap.get(threadPoolName);
        if (null == threadPoolExecutor) return new ThreadPoolConfigEntity(applicatioName,threadPoolName);
        //线程池配置数据
        ThreadPoolConfigEntity threadPoolConfigVO = new ThreadPoolConfigEntity(applicatioName, threadPoolName);
        threadPoolConfigVO.setCorePoolSize(threadPoolExecutor.getCorePoolSize());
        threadPoolConfigVO.setQueueType(threadPoolExecutor.getQueue().getClass().getSimpleName());
        threadPoolConfigVO.setQueueSize(threadPoolExecutor.getQueue().size());
        threadPoolConfigVO.setRemainingCapacity(threadPoolExecutor.getQueue().remainingCapacity());
        if (logger.isDebugEnabled()){
            logger.info("动态线程池，配置查询 应用名：{} 线程池：{} 池化配置：{}",applicatioName,threadPoolName, JSON.toJSONString(threadPoolConfigVO));
        }
        return threadPoolConfigVO;
    }

    @Override
    public void updateThreadPoolConfig(ThreadPoolConfigEntity threadPoolConfigEntity) {
        if (null == threadPoolConfigEntity || !applicatioName.equals(threadPoolConfigEntity.getAppName())) return;
        ThreadPoolExecutor threadPoolExecutor = threadPoolExecutorMap.get(threadPoolConfigEntity.getThreadPoolName());
        if (null == threadPoolExecutor) return;

        //设置参数 【调整核心线程数和最大线程数】
        threadPoolExecutor.setCorePoolSize(threadPoolConfigEntity.getCorePoolSize());
        threadPoolExecutor.setMaximumPoolSize(threadPoolExecutor.getMaximumPoolSize());
    }
}
